<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\Refund;
use App\Models\Order;
use App\Http\Controllers\Controller;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;

class OrdersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.orders'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.orders'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.orders'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.orders'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order);
        $grid->model()->select('orders.*', 'users.username', 'users.nick_name', 'users.avatar')->join('users', 'user_id', 'users.id');
        $grid->id('Id')->totalRow(trans('admin.total'));
        $grid->no(trans('admin.no'));
        $grid->username(trans('admin.username'))->modal(trans('admin.detail'), function ($model) {
            $data = $model->user()->first()->wallet()->select('balance')->first();

            $rows = [
                trans('admin.balance') => $data->balance,
            ];

            return new Table([trans('admin.wallet_info'), trans('admin.value')], $rows);

        });
        $grid->nick_name(trans('admin.nick_name'));
        $grid->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->contact_name(trans('admin.contact_name'));
        $grid->contact_phone(trans('admin.contact_phone'));
        $grid->scheduled_time(trans('admin.scheduled_time'))->display(function ($value) {
            return $this->schedule->begin_time . '--' . $this->schedule->end_time;
        });
        $grid->total_amount(trans('admin.total_amount'))->totalRow(function ($amount) {
            return "<span class='text-danger text-bold'><i class='fa fa-dollar'></i> {$amount} </span>";
        });
        $grid->paid_at(trans('admin.paid_at'));
        $grid->refund_at(trans('admin.refund_at'));
        $grid->status(trans('admin.status'))->display(function ($value) {
            if ($this->refund_status != Order::REFUND_STATUS_PENDING) {
                return Order::$REFUND_STATUS_MAP[$this->refund_status];
            }

            if ($this->closed) {
                return Order::CLOSED;
            }

            return Order::$STATUS_MAP[$value];
        });
        $grid->remark(trans('admin.remark'));
        $grid->created_at(trans('admin.created_at'));
//        $grid->updated_at(trans('admin.updated_at'));
        $grid->disableRowSelector();
//        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->column(1 / 3, function ($filter) {
                $filter->equal('no', trans('admin.no'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('username', trans('admin.username'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->between('total_amount', trans('admin.amount'));
            });
        });
        $grid->actions(function ($action) {
            $action->disableEdit();
            if ($action->row->refund_status == Order::REFUND_STATUS_UNDER_REVIEW || $action->row->refund_status == Order::REFUND_STATUS_APPLIED) {
                $action->prepend(new Refund($action->getKey()));
            }
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->id('Id');
        $show->no(trans('admin.no'));
        $show->user(trans('admin.username'))->as(function ($value) {
            return $value->username;
        });
        $show->contact_name(trans('admin.contact_name'));
        $show->contact_phone(trans('admin.contact_phone'));
        $show->schedule(trans('admin.scheduled_time'))->as(function ($value) {
            return $value->begin_time . '--' . $value->end_time;
        });
        $show->total_amount(trans('admin.total_amount'));
        $show->paid_at(trans('admin.paid_at'));
        $show->refund_status(trans('admin.refund_status'))->as(function ($value) {
            return Order::$REFUND_STATUS_MAP[$value];
        });
        $show->refund_at(trans('admin.refund_at'));
        $show->status(trans('admin.status'))->as(function ($value) {
            return Order::$STATUS_MAP[$value];
        });
        $show->closed(trans('admin.closed'))->as(function ($value) {
            return Order::$CLOSED_MAP[$value];
        });
        $show->user_distance(trans('admin.user_distance'));
        $show->seller_distance(trans('admin.seller_distance'));
        $show->remark(trans('admin.remark'));
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));
        $show->goods(trans('admin.goods'), function ($goods) use ($show) {
            $goods->setResource('/orders');
            $goods->shop(trans('admin.shop'))->as(function ($value) {
                return $value['name'];
            });
            $goods->title(trans('admin.title'));
            $goods->subtitle(trans('admin.subtitle'));
            $goods->image(trans('admin.image'))->image();
            $goods->content(trans('admin.content'));
            $goods->service_time(trans('admin.service_time'));
            $goods->price(trans('admin.price'));
            $goods->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });
        });
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();

                $tools->disableDelete();
            });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order);

        $form->number('user_id', 'User id');
        $form->text('contact_name', trans('admin.contact_name'));
        $form->mobile('contact_phone', trans('admin.contact_phone'));
        $form->datetime('scheduled_time', trans('admin.scheduled_time'))->default(date('Y-m-d H:i:s'));
        $form->decimal('total_amount', trans('admin.total_amount'))->default(0.00);
        $form->radio('payment_method', trans('admin.payment_method'))->options(Order::$PAYMENT_METHOD_MAP)->default(Order::ALI_PAY);
        $form->text('payment_no', trans('admin.payment_no'));
        $form->radio('status', trans('admin.status'))->options(Order::$STATUS_MAP)->default(Order::STATUS_PENDING);
        $form->switch('closed', trans('admin.closed'));
        $form->textarea('remark', trans('admin.remark'));
        $form->select('goods', trans('admin.goods'));

        return $form;
    }
}
